package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Member;
import cn.itcast.pojo.Order;
import cn.itcast.service.MemberService;
import cn.itcast.service.OrderService;
import cn.itcast.utils.ThreadLocalUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.soap.MimeHeaders;
import java.time.Duration;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {
    
    @Reference
    private OrderService orderService;
    
    @Reference
    private MemberService memberService;
    
    
    @PostMapping("/submit")
    public Result submit(HttpServletRequest request, HttpServletResponse response, @RequestBody Map map){
        
        String phoneNumber = (String) map.get("phoneNumber");
        String validateCode = (String) map.get("validateCode");
        Boolean flag = orderService.checkCode(phoneNumber, validateCode);
        if(flag){
            
            Result result = null;

            try {
                //添加预约类型
                map.put("orderType", Order.ORDERTYPE_WEIXIN);

                //比对成功添加预约信息到数据库中
                result = orderService.saveOrder(map);
            } catch (Exception e) {
                e.printStackTrace();
                return result;
            }
            return result;

        }else {

            //如果短信验证比对不成功，返回结果给页面
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }
    }

    @GetMapping("/{id}")
    
    public Result findById(@PathVariable("id")String id){
        try {
            Order order=orderService.findById(id);
            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS,order);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false,MessageConstant.ORDER_FAIL);
        
    }
}
